import {ColumnMappings, ColumnsExcludeType, DEFAULT_COLUMNS} from '@/config/column'
import {request} from '@/api/service'
import {utils} from "@/util/utils";
import XEUtils from 'xe-utils'

const urlPrefix = '/api/system/menu/'

/**
 *
 * @param vm
 * @param id
 * @returns {*[]}
 */
async function generateColumns(vm, id) {

  const res = await request({
    url: urlPrefix + `${id}/`,
    method: 'get',
  })
  return res.data
}

const getPageOptions = function (options, columns) {
  const pageOptions = XEUtils.clone(options.column_map.pageOptions || {}, true)
  pageOptions.compact = pageOptions.compact ? pageOptions.compact: null;
  pageOptions.export = pageOptions.export ? {local: true}: false
  return {pageOptions}
}

export const crudOptions = async (vm) => {
  let columns = [].concat(DEFAULT_COLUMNS)
  const options = await generateColumns(vm, vm.$route.params.id)
  console.log(options, 66666)
  columns = columns.concat(utils.formatColumns(options.column_map.columns))
  const pageOptions = getPageOptions(options, columns)
  return {
    ...pageOptions,
    options: {
      // tableType: 'vxe-table',
      rowKey: 'id',
      height: '100%', // 表格高度100%, 使用toolbar必须设置
      maxHeight: null,
      highlightCurrentRow: false,
      ...options.column_map.options
    },
    rowHandle: {
      view: {
        thin: true,
        text: '',
        disabled() {
          return !vm.hasPermissions('Retrieve')
        }
      },
      width: 230,
      edit: {
        thin: true,
        text: '',
        disabled() {
          return !vm.hasPermissions('Update')
        }
      },
      remove: {
        thin: true,
        text: '',
        disabled() {
          return !vm.hasPermissions('Delete')
        }
      },
      custom: []

    },
    searchOptions: {
      disabled: !vm.hasPermissions('Search'),
    },
    // indexRow: { // 或者直接传true,不显示title，不居中
    //   title: '序号',
    //   align: 'center',
    //   width: 100
    // },

    viewOptions: {
      componentType: 'form'
    },
    formOptions: {
      defaultSpan: 24, // 默认的表单 span
      isDynamic: true,
      formJson: options.form_design,
      width: '50%',
      maxHeight: true,
      nullToBlankStr: false,
      ...options.column_map.formOptions,
    },
    columns: columns
  }
}
